package com.mesSystem.service.impl.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mesSystem.dto.Result;
import com.mesSystem.entity.Employee;
import com.mesSystem.entity.WorkstationAndEmployee;
import com.mesSystem.entity.WorkstationAndEquipment;
import com.mesSystem.mapper.EmployeeMapper;
import com.mesSystem.mapper.WorkstationAndEmployeeMapper;
import com.mesSystem.service.impl.EmployeeService;
import com.mesSystem.service.impl.WorkstationAndEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@Service
public class WorkstationAndEmployeeServiceImpl extends ServiceImpl<WorkstationAndEmployeeMapper, WorkstationAndEmployee> implements WorkstationAndEmployeeService {
    @Autowired
    EmployeeService employeeService;

    /**
     * 添加员工id到工位员工管理表
     * @param employeeIds 员工id的list集合
     * @param workstationId 工位id
     */
    public void update(List<Long> employeeIds, Long workstationId) {
        // 创建一个WorkstationAndEquipment对象用来对比数据库信息
        WorkstationAndEmployee workstationAndEmployee = new WorkstationAndEmployee();
        workstationAndEmployee.setWorkstationId(workstationId);
        for (Long equipmentId : employeeIds) {
            workstationAndEmployee.setEmployeeId(equipmentId);
            // 如果设备id已存在，则删除
            if (count(new QueryWrapper<>(workstationAndEmployee)) > 0) {
                remove(new QueryWrapper<>(workstationAndEmployee));
            } else {
                // 如果未存在，则添加新的设备id
                save(workstationAndEmployee);
            }
        }
    }

    /**
     * 分页查询通返回不在其他岗位上的员工的信息（在本岗位或无岗位）
     * @param page
     * @param size
     * @param workstationId
     * @return
     */
    public List<Employee> queryAvailableEmployee(Integer page, Integer size, Long workstationId) {
        // 创建分页对象
        Page<Employee> employeePage = new Page<>(page, size);
        // 构造查询条件
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        // 获得所有员工信息，但剔除（非workstationId对应）不在本工位的，在其他岗位上的员工信息
        queryWrapper.ne("employee_id", getEmployeeIdsNonMatchingWorkstationId(workstationId));

        // 执行分页查询
        employeeService.page(employeePage, queryWrapper);
        List<Employee> employees = employeePage.getRecords();

        return employees;
    }

    /**
     * 获得在工位-员工表中，不对应于workstationId的员工id
     * @param workstationId
     * @return
     */
    private List<Long> getEmployeeIdsNonMatchingWorkstationId(Long workstationId) {
        // 在这里查询WorkstationAndEmployee表中的员工ID，根据工位ID筛选出不对应于工位id的员工id
        QueryWrapper<WorkstationAndEmployee> queryWrapper = new QueryWrapper<>();
        queryWrapper.notIn("workstation_id", Collections.singletonList(workstationId));

        List<WorkstationAndEmployee> Employees = list(queryWrapper);
        if (Employees == null) {
            Employees = new ArrayList<>();
        }
        return Employees.stream()
                .map(WorkstationAndEmployee::getEmployeeId)
                .collect(Collectors.toList());
    }

    /**
     * 分页查询工位-员工表中，对于workstationId的员工id
     * 返回 员工信息（id + 姓名）
     * @param page
     * @param size
     * @param workstationId
     * @return
     */
    public List<Employee> queryEmployeeByWorkstationId(Integer page, Integer size, Long workstationId) {
        // 创建分页器对象
        Page<WorkstationAndEmployee> pages = new Page<>(page, size);

        // 创建查询条件，根据 workstationId 查询
        QueryWrapper<WorkstationAndEmployee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("workstation_id", workstationId);

        // 调用分页查询方法，将分页器和查询条件传递给 service
        page(pages, queryWrapper);

        // 从分页器对象中获取分页后的数据
        List<WorkstationAndEmployee> workstations = pages.getRecords();

        // 提取员工ID和姓名并返回
        List<Employee> employeeList = workstations.stream()
                .map(workstationAndEmployee -> {
                    Long employeeId = workstationAndEmployee.getEmployeeId();
                    String employeeName = employeeService.getEmployeeName(employeeId); // 使用之前提到的方法查询员工姓名
                    return new Employee(employeeId, employeeName);
                })
                .collect(Collectors.toList());

        // 返回分页结果
        return employeeList;
    }

}
